package com.infra.server.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infra.server.entity.SysResource;
import com.infra.server.mapper.SysResourceMapper;
import com.infra.server.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gisocn
 * @Date: 2020/10/21
 * @Description:
 **/
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService {

    @Autowired
    private SysResourceService sysResourceService;

    /**
     * 根据具体情况添加资源
     */
    @Override
    public boolean addResource(List<SysResource> list) {
        if (ObjectUtil.isNotEmpty(list)) {
            List<SysResource> saveList = new ArrayList<>();
            for (SysResource s : list) {
                QueryWrapper<SysResource> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name",s.getName());
                SysResource sysResource = sysResourceService.getOne(queryWrapper);
                if (ObjectUtil.isEmpty(sysResource)) {
                    SysResource sysResource1 = new SysResource();
                    sysResource1.setName(s.getName());
                    sysResource1.setDescription(s.getDescription());
                    sysResource1.setResourceType(s.getResourceType());
                    sysResource1.setHttpMethod(s.getHttpMethod());
                    saveList.add(sysResource1);
                }
            }
            if (ObjectUtil.isNotEmpty(saveList)) {
                // 只做插入新的api接口，不做修改
                return sysResourceService.saveBatch(saveList);
            } else {
                return true;
            }
        }
        return false;
    }
}
